<?php

namespace Admin\Controller;

use Common\Controller\AdminBaseController;

/**
 * 后台首页控制器
 */
class WindowController extends AdminBaseController {
    /**
     * 首页
     */
    public function index() {
        $list = M("canteen")->where(array('canteen_status' => 1))->select();
        $this->assign('canteenList', $list);
        $this->display();
    }

    public function delWindow() {
        $member_id = $_POST['member_id'];
        if (!$member_id) {
            returnJson(false, array(), '操作失败，请联系管理员');
        }
        else {
            $updateArray['window_status'] = 0;
            $updateArray['member_id'] = $member_id;
            $res = M("member")->save($updateArray);
            if ($res) {
                returnJson(true, array(), '操作成功');
            }
            else {
                returnJson(false, array(), '操作失败，请联系管理员');
            }
        }
    }


    public function windowList() {
        $canteenList = M("canteen")->where(array('canteen_status' => 1))->select();

        $canteen_id = $_GET['canteen_id'];
        if ($canteen_id) {
            $canteenInfo = M("canteen")->where(array('canteen_id' => $canteen_id))->find();
            $list = M("member")->where(array('window_status' => 1, 'canteen_id' => $canteen_id))->select();
        }
        else {
            $canteenInfo['canteen_name'] = "所有";
            $list = M("member")->where(array('window_status' => 1))->select();
        }
        if (!empty($list)) {
            foreach ($list as $k => $l) {
                $list[$k]['canteen_name'] = getCanteenName($l['canteen_id']);
            }
        }
        $titleName = $canteenInfo['canteen_name'] . "档口";
        $this->assign('canteenList', $canteenList);
        $this->assign('titleName', $titleName);
        $this->assign('windowList', $list);
        $this->display();
    }

    /**
     * elements
     */
    public function editCanteen() {
        if ($_GET['canteen_id']) {
            $canteenInfo = M("canteen")->where(array('canteen_id' => $_GET['canteen_id']))->find();
            returnJson(true, $canteenInfo);
        }
        elseif ($_POST['canteen_id']) {
            $updateArray = array();
            $updateArray['canteen_id'] = $_POST['canteen_id'];
            $updateArray['canteen_name'] = $_POST['canteen_name'];
            $updateArray['canteen_contact_name'] = $_POST['canteen_contact_name'];
            $updateArray['canteen_contact_phone'] = $_POST['canteen_contact_phone'];
            $updateArray['edittime'] = time();
            $res = M("canteen")->save($updateArray);
            if ($res) {
                returnJson(true, array());

            }
            else {
                returnJson(false, array(), '操作失败，请联系管理员');

            }
            exit;
        }
    }

    /**
     * elements
     */
    public function editWindow() {
        if ($_GET['member_id']) {
            $windowInfo = M("member")->where(array('member_id' => $_GET['member_id']))->find();
            returnJson(true, $windowInfo);
        }
        elseif ($_POST['member_id']) {


            $updateArray = array();
            $updateArray['member_id'] = $_POST['member_id'];
            $updateArray['window_name'] = trim($_POST['window_name']);
            $updateArray['window_contact_name'] = $_POST['window_contact_name'];
            $updateArray['window_contact_phone'] = $_POST['window_contact_phone'];
            $password = $_POST['password'];
            if ($password) {
                $updateArray['salt_value'] = makeSalt();
                $updateArray['password'] = md5($password .$updateArray['salt_value']);
            }
            $res = M("member")->save($updateArray);
            if ($res) {
                returnJson(true, array());

            }
            else {
                returnJson(false, array(), '操作失败，请联系管理员');

            }
            exit;
        }
    }

    public function addWindow() {

        $inserArr = array();
        $inserArr['username'] = trim($_POST['username']);
        //查重
        $lits = M("member")->where(array('username' => trim($_POST['username'])))->select();
        if (!empty($lits)) {
            $this->error('添加档口失败，登陆账号重复,请选择其他登陆账号');
        }
        $inserArr['salt_value'] = makeSalt();
        $inserArr['password'] = md5($_POST['password'] . $inserArr['salt_value']);
        $inserArr['register_time'] = time();
        $inserArr['canteen_id'] = $_POST['canteen_id'];
        $inserArr['window_status'] = 1;
        $inserArr['window_contact_name'] = $_POST['window_contact_name'];
        $inserArr['window_name'] = $_POST['window_name'];
        $inserArr['window_contact_phone'] = $_POST['window_contact_phone'];
        $res = M("member")->add($inserArr);
        if ($res) {
            $this->success('添加成功', U('/Admin/Window/WindowList'));

        }
        else {
            $this->error('添加档口失败，请联系管理员');

        }
    }

    /**
     * welcome
     */
    public function addCanteen() {
        $canteen_name = trim($_POST['canteen_name']);
        $canteen_contact_name = trim($_POST['canteen_contact_name']);
        $canteen_contact_phone = trim($_POST['canteen_contact_phone']);
        if (!$canteen_name) {
            returnJson(false, array(), '请填写正确的食堂名称');
        }
        if (!$canteen_contact_name) {
            returnJson(false, array(), '请填写食堂联系人');
        }
        if (!$canteen_contact_phone) {
            returnJson(false, array(), '请填写食堂联系电话');
        }
        $canteenData = array();
        $canteenData['canteen_name'] = $canteen_name;
        $canteenData['edittime'] = time();
        $canteenData['canteen_status'] = 1;
        $canteenData['canteen_contact_name'] = $canteen_contact_name;
        $canteenData['canteen_contact_phone'] = $canteen_contact_phone;
        $res = M("canteen")->add($canteenData);
        if ($res) {
            returnJson(true, array());
        }
        else {
            returnJson(false, array(), '操作失败，请联系管理员');
        }

    }

    public function delCanteen() {
        $canteen_id = $_POST['canteen_id'];
        if (!$canteen_id) {
            returnJson(false, array(), '操作失败，请联系管理员');

        }
        else {
            $updateArray['canteen_status'] = 0;
            $updateArray['canteen_id'] = $canteen_id;
            $res = M("canteen")->save($updateArray);
            if ($res) {
                returnJson(true, array(), '操作成功');
            }
            else {
                returnJson(false, array(), '操作失败，请联系管理员');
            }
        }
    }


}
